<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.logistics_delete"
                   @click="handleDelete">删 除
        </el-button>

      </template>
      <template slot-scope="{type,disabled}" slot="warehouseIdForm">
        <el-input  v-model="form.warehouseId"></el-input>
        <avue-input-table
          style="margin-top: 10px;width: 40%"
          v-if="type!='view'"
          :props="wProps"
          :column="warehouseOp"
          :on-load="wOnLoad"
          v-model="form.warehouseId"
          ref="warehouse"
          placeholder="点击快速选择" >
        </avue-input-table>
      </template>
      <template slot="menuLeft">
      <el-tag type="info">历史操作</el-tag>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
        icon="el-icon-menu"
        size="small"
        type="text"
        style="margin-left: 10px"
        @click="showDetail(scope.row.id)">查看货物
      </el-button>
      </template>
    </avue-crud>
    <el-dialog title="所有货物"
    :visible.sync="detailBox"
    width="70%">
      <avue-crud :option="detailOption"
                 :table-loading="detailLoading"
                 :data="detailData"
                 :page="detailPage"
                 v-model="detailForm"
                 ref="detailCrud"
                 @row-update="detailRowUpdate"
                 @row-del="detailRowDel"
                 @search-change="detailSearchChange"
                 @search-reset="detailSearchReset"
                 @current-change="detailCurrentChange"
                 @size-change="detailSizeChange"
                 @on-load="detailOnLoad">
        <template slot="menuLeft">
          <el-button type="danger"
                     size="small"
                     icon="el-icon-delete"
                     plain
                     @click="handleDelete">删 除
          </el-button>
        </template>
      </avue-crud>

    </el-dialog>

  </basic-container>
</template>

<script>
  import {getPage,audit, getDetail, add, update, remove , getWareList} from "@/api/warehouse/logistics";
  import {getAll} from "@/api/warehouse/warehouse";
  import {mapGetters} from "vuex";
  import {getDetailPage,detailUpdate,detailRemove} from "@/api/warehouse/logisticsDetail";

  export default {
    data() {
      return {

        //以下为查看详细货物所需数据
        detailData:[],
        logisticsId:0,
        detailBox:false,
        detailForm: {},
        detailQuery: {},
        detailLoading: true,
        detailPage: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        detailOption: {    excelBtn:true,
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          addBtn: false,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "商品名",
              prop: "spuTitle",
              search:true,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }],
              editDisplay:false,
            },
            {
              label: "商品图片",
              prop: "spuThumb",
              type:'img',
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }],
              editDisplay:false,
            },
            {
              label: "规格",
              prop: "saleAttr",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }],
              editDisplay:false,
            },
            {
              label: "入库数量",
              prop: "stockNumber",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
          ]
        },

        //
        wProps: {
          label: 'warehouseName',
          value: 'id',
        },

        warehouseOp: {
          label: "仓库",
          prop: "warehouseId",
          span:24,
          children: {
            border: true,
            index: true,
            viewBtn: true,
            calcHeight: 210,
            column: [
              {
                label: '仓库名',
                width: 120,
                search:true,
                searchSpan:5,
                prop: 'warehouseName'
              }, {
                label: "类型",
                prop: "warehouseType",
                type: "select",
                props: {
                  label: "dictValue",
                  value: "dictKey"
                },
                search:true,
                dicUrl: "/api/ocm-system/dict/dictionary?code=warehouse_type",
                rules: [{
                  required: true,
                  message: "请输入0 官方仓库 1个人仓库 2合作仓库",
                  trigger: "blur"
                }]
              }, {
                label: "验证",
                prop: "oauth",
                type: 'select',
                search: true,
                dicData: [{
                  title:'无需密码',
                  value:0
                },{
                  title:'需要密码',
                  value:1
                }],
                props: {
                  label: "title",
                  value: "value"
                }
              },
              {
                label: '所有者',
                prop: 'warehouseOwner'
              }],
          },
          type:'table',
          placeholder:'请选择仓库',
          rules: [{
            required: true,
            message: "请选择仓库",
            trigger: "click"
          }]
        },
        form:{
          warehouseId:'0',
          details:[],
        },
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {    excelBtn:true,
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          selection: true,
          viewBtn:true,
          column: [
            {
              label: "运单号",
              prop: "id",
              addDisplay: false,
              editDisplay: false,
              rules: [{
                required: true,
                message: "请输入运单号",
                trigger: "blur"
              }]
            },
            {
              label: "仓库",
              editDisplay: false,
              prop: "warehouseName",
              rules: [{
                required: true,
                message: "请选择仓库",
                trigger: "blur"
              }]
            },
            {
              label: "仓库",
              hide:true,
              editDisplay: false,
              viewDisplay:false,
              search: true,
              formslot:true,
              prop: "warehouseId",
              searchMenuSpan: 6,
              children: {
                border: true,
                index: true,
                viewBtn: true,
                column: [
                  {
                    label: '仓库名',
                    width: 120,
                    search:true,
                    searchSpan:24,
                    prop: 'warehouseName'
                  }, {
                    label: "类型",
                    prop: "warehouseType",
                    type: "select",
                    props: {
                      label: "dictValue",
                      value: "dictKey"
                    },
                    search:true,
                    dicUrl: "/api/ocm-system/dict/dictionary?code=warehouse_type",
                    rules: [{
                      required: true,
                      message: "请输入0 官方仓库 1个人仓库 2合作仓库",
                      trigger: "blur"
                    }]
                  }, {
                    label: '所有者',
                    prop: 'warehouseOwner'
                  }],
              },

              onLoad: ({ page, value,data }, callback) => {
                //bug影响 仅能加此代码消除
                if (value) {
                  return
                }
                //bug结束
                let wareData=[];
                if("undefined" ==typeof page){
                  page={
                    currentPage:1,
                    pageSize:10
                  }
                }
                getWareList(page.currentPage, page.pageSize,data).then(res => {
                  wareData = res.data.data;
                  callback({
                    total: wareData.total,
                    data:  wareData.records
                  })
                });
              },
              props: {
                label: "warehouseName",
                value: "id"
              },
              type:'table',
              placeholder:'请选择仓库',
              rules: [{
                required: true,
                message: "请选择仓库",
                trigger: "blur"
              }]
            },
            {
              label: "物流单号",
              prop: "logisticsNumber",
              rules: [{
                required: true,
                message: "请输入物流单号",
                trigger: "blur"
              }]
            },
            {
              label: "包裹重量",
              prop: "logisticsWeight",
              rules: [{
                required: true,
                message: "请输入重量",
                trigger: "blur"
              }]
            },
            {
              label: "联系电话",
              prop: "logisticsPhone",
              tip:'联系不上您可能会导致运单退回',
              rules: [{
                required: true,
                message: "请输入联系电话",
                trigger: "blur"
              }]
            },
            {
              label: "当前状态",
              search:true,
              prop: "status",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              disabled:true,
              dicUrl: "/api/ocm-system/dict/dictionary?code=logistics_status",
              type: 'select',
              rules: [{
                required: true,
                message: "请输入0已下架 1待上架 2审核中 3已上架",
                trigger: "blur"
              }]
            },
            {
              label: "运单备注",
              prop: "remark",
              rules: [{
                required: false,
                message: "请输入创建者备注",
                trigger: "blur"
              }]
            },
            {
              label: "审核备注",
              editDisplay: false,
              prop: "auditRemark",
              addDisplay:false,
              rules: [{
                required: false,
                message: "请输入审核员备注",
                trigger: "blur"
              }]
            },

          ]
        },
        data: []
      };
    },
    created() {

    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.logistics_add, false),
          viewBtn: this.vaildData(this.permission.logistics_view, false),
          delBtn: this.vaildData(this.permission.logistics_delete, false),
          editBtn: this.vaildData(this.permission.logistics_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      //以下为运单详情所需方法
      // detailRowSave(row, done, loading) {
      //   add(row).then(() => {
      //     done();
      //     this.onLoad(this.page);
      //     this.$message({
      //       type: "success",
      //       message: "操作成功!"
      //     });
      //   }, error => {
      //     window.console.log(error);
      //     loading();
      //   });
      // },
      detailRowUpdate(row, index, done, loading) {
        detailUpdate(row).then(() => {
          done();
          this.detailOnLoad(this.detailPage);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      detailRowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return detailRemove(row.id);
          })
          .then(() => {
            this.detailOnLoad(this.detailPage);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      detailSearchReset() {
        this.detailQuery = {};
        this.detailOnLoad(this.detailPage);
      },
      detailSearchChange(params, done) {
        this.detailQuery = params;
        this.detailPage.currentPage = 1;
        this.detailOnLoad(this.page, params);
        done();
      },
      detailCurrentChange(currentPage){
        this.detailPage.currentPage = currentPage;
      },
      detailSizeChange(pageSize){
        this.detailPage.pageSize = pageSize;
      },
      detailOnLoad(page, params = {}) {
        this.detailLoading = true;
        getDetailPage(page.currentPage, page.pageSize, Object.assign(params,{'logisticsId_equal':this.logisticsId}, this.query)).then(res => {
          const data = res.data.data;
          this.detailPage.total = data.total;
          this.detailData = data.records;
          this.detailLoading = false;
        });
      },
      //
      showDetail(id){
        this.logisticsId=id
        this.detailOnLoad(this.detailPage)
        this.detailBox=true
      },
      // rowSave(row, done, loading) {
      //   add(row).then(() => {
      //     done();
      //     this.onLoad(this.page);
      //     this.$message({
      //       type: "success",
      //       message: "操作成功!"
      //     });
      //   }, error => {
      //     window.console.log(error);
      //     loading();
      //   });
      // },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
        }
        done();
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        if(this.query.warehouseId!=null){
          this.query.warehouseId_equal=this.query.warehouseId
          this.query.warehouseId=null
        }
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getPage(page.currentPage, page.pageSize, Object.assign(params,{'l.createUser':'0'}, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
      wOnLoad: ({ page,value,data }, callback) => {
        if("undefined" === typeof page){
          page={
            currentPage:1,
            pageSize:10
          }
        }
        let wareData=[];
        getAll(page.currentPage, page.pageSize,Object.assign({}, data)).then(res => {
          wareData = res.data.data;
          callback({
            total: wareData.total,
            data:  wareData.records
          })
        });
      },
    }
  };
</script>

<style>
</style>
